Change default serialization units - #313
Draft
kylecarow wants to merge 4 commits into
Draft
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Built on #309
1. New canonical serialization units (proc macro:
quantity_config)The following quantities now serialize in practical engineering units instead of the SI base unit. The raw uom float is in the SI base, so each requires a new
serialize_withhelper to convert on write:EnergyPowerPressureSpecificEnergySpecificPowerTemperatureTemperatureIntervalThe Ratio "bare name legacy escape hatch" was also removed — Ratio fields now get the standard
field_ratio/field_percenttreatment like every other SI quantity.2. New
serialize_withhelpers (serde_helpers.rs)Seven new
impl_si_serialize_as!expansions to support the above:energy_as_kilojoules,energy_as_kilowatt_hours,power_as_kilowatts,pressure_as_kilopascals,ratio_as_percent,specific_energy_as_kilowatt_hours_per_kilogram,specific_power_as_kilowatts_per_kilogram,temperature_as_degrees_celsius,temperature_interval_as_degrees_celsius.3. Field-level
#[si_unit(...)]overrides on struct fieldsAcross vehicle_model.rs, chassis.rs, conv.rs,
hev.rs,hvac_*.rs,electric_machine.rs, fuel_converter.rs,fuel_storage.rs,transmission.rs, and reversible_energy_storage.rs — fields where the new global default isn't the right choice are overridden with an explicit#[si_unit(...)]annotation (e.g.#[si_unit(unitless)]for drag/rolling-resistance coefficients that should serialize as bare dimensionless ratios,#[si_unit(percent)]for SOC and efficiency fields).#[si_unit]CyclegradeVec<si::Ratio>percentCycleElementgradesi::RatiopercentCycle.gradeChassisdrag_coefsi::RatiounitlessChassiswheel_rr_coefsi::RatiounitlessChassiswheel_fric_coefsi::RatiounitlessConventionalVehiclealt_effsi::RatiopercentRGWDBStatesoc_fc_on_bufferTrackedState<si::Ratio>percentRESGreedyWithDynamicBuffersspeed_soc_disch_buffer_coeffOption<si::Ratio>percentRESGreedyWithDynamicBuffersspeed_soc_fc_on_buffer_coeffOption<si::Ratio>percentRESGreedyWithDynamicBuffersspeed_soc_regen_buffer_coeffOption<si::Ratio>percentHEVStopStartControlsoc_fc_forced_onOption<si::Ratio>percentHVACSystemForLumpedCabinStatecopTrackedState<Option<si::Ratio>>unitlessHVACSystemForLumpedCabinAndRESStatecopTrackedState<Option<si::Ratio>>unitlessElectricMachineStateeffTrackedState<si::Ratio>percentElectricMachineStateeff_fwd_at_max_inputTrackedState<si::Ratio>percentElectricMachineStateeff_at_max_regenTrackedState<si::Ratio>percentFuelConverterStateeffTrackedState<si::Ratio>percentFuelConverterThermalStateeff_coeffTrackedState<si::Ratio>percentFuelStorageenergy_capacitysi::Energykilowatt_hoursReversibleEnergyStorageenergy_capacitysi::Energykilowatt_hoursReversibleEnergyStoragemin_socsi::RatiopercentReversibleEnergyStoragemax_socsi::RatiopercentReversibleEnergyStorageStatesocTrackedState<si::Ratio>percentReversibleEnergyStorageStatesoc_regen_bufferTrackedState<si::Ratio>percentReversibleEnergyStorageStatesoc_disch_bufferTrackedState<si::Ratio>percentReversibleEnergyStorageStateeffTrackedState<si::Ratio>percentReversibleEnergyStorageStatesohTrackedState<si::Ratio>percentTrackedState<f64>)TransmissionStateeffTrackedState<si::Ratio>percentVehicleStategrade_currTrackedState<si::Ratio>percentThe core distinction between
unitlessandpercentis physical meaning: dimensionless coefficients used in multiplication (drag, rolling resistance, friction, COP) serialize with their raw value and getunitless; fractions that represent a proportion of a maximum (efficiency, SOC, grade) serialize as percentages.4.
sohfield promoted tosi::RatioIn
ReversibleEnergyStorage,sohwas a rawTrackedState<f64>— it's nowTrackedState<si::Ratio>with#[si_unit(percent)], giving it the same dimensional-safety and multi-unit deserialization that all other SI fields have.5. Integration test updates
tests/test_serde_api_integration.rsupdated to match the new canonical key names (e.g.energy_capacity_kilowatt_hoursinstead ofenergy_capacity_joules,min_soc_percentinstead ofmin_soc_ratio).TODO: perhaps fields like friction/drag/rr coefficients should never have been ratios?